package com.huishu.tcm.controller;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.huishu.tcm.entity.User;
import com.huishu.tcm.util.exception.TcmException;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huishu.tcm.entity.PrescriptionOther;
import com.huishu.tcm.service.PrescriptionOtherService;
import com.huishu.tcm.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "其他处方")
@RestController
@RequestMapping("/api/prescriptionOther")
public class PrescriptionOtherController extends BaseController<PrescriptionOtherService> {

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键查询一条数据", notes = "根据主键查询一条数据")
    @RequestMapping(method = RequestMethod.GET)
    public PrescriptionOther get(@RequestParam String id) throws TcmException {
        QueryWrapper<PrescriptionOther> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        return service.getOne(wrapper);
    }

    /**
     * 查询数据列表
     * @return
     */
    @ApiOperation(value = "根据筛选条件查询数据列表", notes = "根据筛选条件查询数据列表")
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public List<PrescriptionOther> list(@RequestBody PrescriptionOther v) throws TcmException {
        QueryWrapper<PrescriptionOther> queryWrapper = service.getQueryWrapper(v);
        return service.list(queryWrapper);
    }

    /**
     * 分页查询数据列表
     * @param pageVo
     * @return
     */
    @ApiOperation(value = "根据筛选条件分页查询数据列表", notes = "根据筛选条件分页查询数据列表")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Page<PrescriptionOther> page(@RequestBody PageVo<PrescriptionOther> pageVo) throws TcmException {
        PrescriptionOther v = pageVo.getEntity();
        QueryWrapper<PrescriptionOther> wrapper = service.getQueryWrapper(v);
        Page<PrescriptionOther> page = new Page<>(pageVo.getPage(),pageVo.getLimit());
        page = service.page(page,wrapper);
        return page;
    }

    /**
     * 添加
     * @param v
     * @return
     */
    @ApiOperation(value = "添加一条数据",notes = "添加一条数据")
    @RequestMapping(method = RequestMethod.POST)
    public boolean save(@RequestBody PrescriptionOther v) throws Exception {
        QueryWrapper<PrescriptionOther> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("patient_id",v.getPatientId());
        queryWrapper.orderByDesc("number");
        List<PrescriptionOther> list = service.list(queryWrapper);
        if(ObjectUtil.isEmpty(list)){
            v.setNumber(1);
        }else{
            v.setNumber(list.get(0).getNumber() + 1);
        }
        Date now = new Date();
        User loginUser = getLoginUser();
        v.setCreateTime(now);
        v.setCreateUser(loginUser.getName());
        v.setUpdateTime(now);
        v.setUpdateUser(loginUser.getName());
        return service.save(v);
    }

    /**
     * 根据主键更新
     * @param v
     * @return
     */
    @ApiOperation(value = "根据主键更新数据",notes = "根据主键更新数据")
    @RequestMapping(method = RequestMethod.PUT)
    public boolean update(@RequestBody PrescriptionOther v) throws Exception {
        Date now = new Date();
        v.setUpdateTime(now);
        v.setUpdateUser(getLoginUser().getName());
        QueryWrapper<PrescriptionOther> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", v.getId());
        return service.update(v,queryWrapper);
    }


    /**
     * 根据主键删除
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键删除数据",notes = "根据主键删除数据")
    @RequestMapping(method = RequestMethod.DELETE)
    public boolean removeById(@RequestParam Integer id){
        QueryWrapper<PrescriptionOther> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return service.remove(wrapper);
    }

}